package com.example.parkingproject.service.serviceImp;

import com.example.parkingproject.code.ServiceCode;
import com.example.parkingproject.ex.ServiceException;
import com.example.parkingproject.jopo.dto.MouthUserAddDto;
import com.example.parkingproject.jopo.dto.MouthUserUpdateDto;
import com.example.parkingproject.jopo.entity.MouthUser;
import com.example.parkingproject.jopo.vo.MouthUserVo;
import com.example.parkingproject.mapper.MouthUserMapper;
import com.example.parkingproject.service.MouthUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class MouthUserServiceImp implements MouthUserService {
    @Autowired
    MouthUserMapper mouthUserMapper;

    /**
     * 添加用户处理
     * @param mouthUserAddDto
     */
    @Override
    public void addMouthUser(MouthUserAddDto mouthUserAddDto) {
        String phone = mouthUserMapper.getPhone(mouthUserAddDto.getCardId());
        log.debug("参数{}",phone);
        mouthUserAddDto.setPhone(phone);
        log.debug("开始处理月租用户添加 参数{}", mouthUserAddDto);
        MouthUserVo result = mouthUserMapper.getMouthUserVoByCardId(mouthUserAddDto.getCardId());
        if (result!=null){
            String message ="用户已经存在";
            throw new ServiceException(ServiceCode.IS_EXISTENCE,message);
        }
        MouthUser user = new MouthUser();
        BeanUtils.copyProperties(mouthUserAddDto,user);
        Date start = new Date();
        user.setCreateTime(start);
        Long end = start.getTime()+1000*60*60*24*30L;
        user.setDeleteTime(new Date(end));
        log.debug("添加参数{}",user);
        mouthUserMapper.addMouthUser(user);
        log.debug("添加处理完毕");
        mouthUserMapper.addspace(user);
        log.debug("传入参数{}",mouthUserAddDto.getCardId());
        mouthUserMapper.setCar(mouthUserAddDto.getCardId());
    }

    /**
     * 查询所有月租用户集合处理
     * @return
     */
    @Override
    public List<MouthUserVo> MouthUserList() {
        log.debug("开始查询月租用户");
        List<MouthUserVo> list = mouthUserMapper.list();
        log.debug("查询到的集合{}",list.toString());
        return list;
    }

    /**
     * 逻辑删除月租用户处理
     * @param cardId
     */
    @Override
    public void deleteMouthUser(String cardId) {
        MouthUserVo mouthUserVoByCardId = mouthUserMapper.getMouthUserVoByCardId(cardId);
        log.debug("处理用户参数{}",cardId);
        if (mouthUserVoByCardId==null){
            String message = "用户数据不存在可能已经删除";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        int i = mouthUserMapper.updateMouthUser(cardId);
        log.debug("逻辑删除用户影响条数{}条",i);
        mouthUserMapper.updateSpace(cardId);

    }

    /**
     * 获取单个月租用户处理
     * @param cardId
     * @return
     */
    @Override
    public MouthUserVo getMouthUserByCard(String cardId) {
        MouthUserVo mouthUserVoByCardId = mouthUserMapper.getMouthUserVoByCardId(cardId);
        log.debug("查询出来的参数{}",mouthUserVoByCardId);
        if (mouthUserVoByCardId==null){
            String message ="用户不存在，不能更改";
            throw  new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        return mouthUserVoByCardId;
    }

    /**
     * 对于单个月租用户进行修改
     * @param mouthUserUpdateDto
     */
    @Override
    public void updateMouthUserAll(MouthUserUpdateDto mouthUserUpdateDto) {
        int i = mouthUserMapper.updateMouthUserAll(mouthUserUpdateDto);
        log.debug("受影响信息{}条",i);
    }

    @Override
    public List<MouthUserVo> search(MouthUserAddDto mouthUserAddDto) {
        List<MouthUserVo> list = mouthUserMapper.search(mouthUserAddDto);
        if (list.size()==0){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"没有查询到数据");
        }
        return list;
    }


}
